/*
 * @lc app=leetcode.cn id=806 lang=cpp
 *
 * [806] 写字符串需要的行数
 */

// @lc code=start
#include <bits/stdc++.h>
using namespace std;
class Solution
{
public:
    vector<int> numberOfLines(vector<int> &widths, string s)
    {
        int all_width = 0, last_width;
        int num_lines = 0;
        for (auto &c : s)
        {
            all_width += widths[c - 'a'];
            if (all_width == 100)
            {
                last_width = all_width;
                num_lines++;
                all_width = 0;
            }
            else if(all_width > 100)
            {
                last_width = all_width - widths[c-'a'];
                all_width = widths[c-'a'];
                num_lines++;
            }
        }

        vector<int> ans{all_width?num_lines+1:num_lines, all_width?all_width:last_width};
        return ans;
    }
};
// @lc code=end
